/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.store.client.components;

import com.elf.gwt.store.client.GWTCommerceConstants;
import com.elf.gwt.store.client.StoreComponentController;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.InlineHTML;
import com.google.gwt.user.client.ui.InlineLabel;


/**
 * 
 */

public class HeaderComponent extends StoreSimpleComponent
{
	protected int currentAccountId = -1;
	protected Grid bodyPanel;
	protected HorizontalPanel leftPanel;
	protected HorizontalPanel rightPanel;
	protected InlineHTML delim1;
	protected InlineLabel logoff;
	protected InlineLabel logon;
	protected InlineLabel myAccount;
	protected InlineLabel createAccount;
	/**
	 * Constructor
	 */

	public HeaderComponent(StoreComponentController controller, String stylePrefix)
	{
		super(controller, stylePrefix, null, false);
		GWTCommerceConstants constants = controller.getStoreConstants();

		bodyPanel = new Grid(1, 2);
		bodyPanel.addStyleName(getHandle() + "BodyPanel");
		leftPanel = new HorizontalPanel();
		leftPanel.addStyleName(getHandle() + "LeftPanel");
		rightPanel = new HorizontalPanel();
		rightPanel.addStyleName(getHandle() + "RightPanel");
		bodyPanel.setWidget(0, 0, leftPanel);
		bodyPanel.setWidget(0, 1, rightPanel);
		bodyPanel.getCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_RIGHT);
		bodyPanel.getCellFormatter().setVerticalAlignment(0, 1, HasVerticalAlignment.ALIGN_MIDDLE);

		Image image = new Image();
		image.setUrl(getImageDirectory() + "logo.jpg");
		leftPanel.add(image);
		panel.setWidget(1, 0, bodyPanel);
		panel.getCellFormatter().setHorizontalAlignment(1, 0, HasHorizontalAlignment.ALIGN_CENTER);

		delim1 = new InlineHTML("&nbsp;" + constants.HeaderComponentDelimiter() + "&nbsp;");
		delim1.setStyleName(getHandle() + "Delimiter");

		InlineHTML delim2 = new InlineHTML("&nbsp;" + constants.HeaderComponentDelimiter() + "&nbsp;");
		delim2.setStyleName(getHandle() + "Delimiter");
		InlineLabel cart = new InlineLabel(constants.HeaderComponentCart());
		cart.setStyleName("ComponentLink");
		cart.addStyleName(getHandle() + "Link");
		cart.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				Window.scrollTo(0, 0);
				onSelection("CartComponent");
			}
		});
		rightPanel.add(delim2);
		rightPanel.add(cart);

		InlineHTML delim3 = new InlineHTML("&nbsp;" + constants.HeaderComponentDelimiter() + "&nbsp;");
		delim3.setStyleName(getHandle() + "Delimiter");
		InlineLabel checkout = new InlineLabel(constants.HeaderComponentCheckout());
		checkout.setStyleName("ComponentLink");
		checkout.addStyleName(getHandle() + "Link");
		checkout.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				Window.scrollTo(0, 0);
				onSelection("CheckoutComponent");
			}
		});
		rightPanel.add(delim3);
		rightPanel.add(checkout);
	}

	public void build()
	{
		int id = StoreComponentController.accountComponent.getAccountModel().getId();
		
		if (id != currentAccountId)
		{
			currentAccountId = id;
			GWTCommerceConstants constants = storeController.getStoreConstants();
			//Log On/Off  |  My Account  |  Cart Contents  |  Checkout  !  Blog  
			if (id > 0)
			{
				if (logon != null)
				{
					rightPanel.remove(createAccount);
					rightPanel.remove(delim1);
					rightPanel.remove(logon);
				}
				if (logoff == null)
				{
					logoff = new InlineLabel(constants.HeaderComponentLogoff());
					logoff.setStyleName("ComponentLink");
					logoff.addStyleName(getHandle() + "Link");
					logoff.addClickHandler(new ClickHandler() {
						public void onClick(ClickEvent event) {
							onSelection("AccountComponent--LogoffAccount");
						}
					});

					myAccount = new InlineLabel(constants.HeaderComponentMyAccount());
					myAccount.setStyleName("ComponentLink");
					myAccount.addStyleName(getHandle() + "Link");
					myAccount.addClickHandler(new ClickHandler() {
						public void onClick(ClickEvent event) {
							onSelection("AccountComponent--ViewAccount");
						}
					});
				}

				rightPanel.insert(myAccount, 0);
				rightPanel.insert(delim1, 0);
				rightPanel.insert(logoff, 0);
			}
			else
			{
				if (logoff != null)
				{
					rightPanel.remove(myAccount);
					rightPanel.remove(delim1);
					rightPanel.remove(logoff);
				}
				if (logon == null)
				{
					logon = new InlineLabel(constants.HeaderComponentLogon());
					logon.setStyleName("ComponentLink");
					logon.addStyleName(getHandle() + "Link");
					logon.addClickHandler(new ClickHandler() {
						public void onClick(ClickEvent event) {
							onSelection("AccountComponent--LoginAccount");
						}
					});

					createAccount = new InlineLabel(constants.HeaderComponentCreateAccount());
					createAccount.setStyleName("ComponentLink");
					createAccount.addStyleName(getHandle() + "Link");
					createAccount.addClickHandler(new ClickHandler() {
						public void onClick(ClickEvent event) {
							onSelection("AccountComponent--CreateAccount");
						}
					});
				}

				rightPanel.insert(createAccount, 0);
				rightPanel.insert(delim1, 0);
				rightPanel.insert(logon, 0);
			}
		}
		onBuilt();
	}
}
